﻿using AgNet.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AgNet.Tests
{
    [TestClass]
    public class FetchResourceListTest
    {
        private static AgNetClient client;
        private static AgSession session;

        [ClassInitialize]
        public static void InitializeClient(TestContext context)
        {
            client = new AgNetClient(TestVariables.ClientId, TestVariables.AppSecret);
            session = client.Login(TestVariables.Email, TestVariables.Password);
        }

        [TestMethod]
        public void CanFetchAlbums()
        {
           this.FetchResource(ResourceTypes.albums);            
        }

        [TestMethod]
        public void CanFetchArtists()
        {
            this.FetchResource(ResourceTypes.artists);
        }

        [TestMethod]
        public void CanFetchGenres()
        {
            this.FetchResource(ResourceTypes.genres);
        }

        [TestMethod]
        public void CanFetchPlayLists()
        {
            this.FetchResource(ResourceTypes.playlists);
        }

        [TestMethod]
        public void CanFetchImageSizes()
        {
            this.FetchImageSize(ResourceTypes.albums, ImageSizes.small);
            this.FetchImageSize(ResourceTypes.albums, ImageSizes.medium);
            this.FetchImageSize(ResourceTypes.albums, ImageSizes.large);
        }

        private void FetchImageSize(ResourceTypes resourceType, ImageSizes size)
        {
            var resourceList = client.FetchResourceList(resourceType, 0, size, session);
            Assert.IsNotNull(resourceList);
            CheckResponseBase(resourceList);
        }

        private void FetchResource(ResourceTypes resourceType)
        {
            int offset = 0;
            int nextOffset = int.MaxValue;

            while (nextOffset != 0)
            {
                var resourceList = client.FetchResourceList(resourceType, offset, ImageSizes.small, session);
                Assert.IsNotNull(resourceList);
                CheckResponseBase(resourceList);
                nextOffset = resourceList.NextOffset;
                offset = nextOffset;
            }            
        }

        private static void CheckResponseBase(AgResponseBase response)
        {
            Assert.IsNotNull(response.Etag);
            Assert.IsFalse(response.HelperStatus == HelperStatuses.invalid);
            Assert.IsFalse(response.Type == ResourceTypes.invalid);
        }
    }
}
